<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>优先级队列</title>
</head>
<body>
<script>
  function PriorityQueue() {
    function QueueElement(element, priority) {
      this.element = element
      this.priority = priority
    }
    this.items = []
    //1. 插入方法
    PriorityQueue.prototype.enqueue = function (element, priority) {
      let queueElement = new QueueElement(element, priority)
      if (this.items.length === 0) {
        this.items.push(queueElement)
      } else {
        let added = false
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].priority > priority) {
            this.items.splice(i, 0, queueElement)
            added = true
            break
          }
        }
        if (!added) {
          this.items.push(queueElement)
        }
      }
    }
    PriorityQueue.prototype.dequeue = function () {
      return this.items.shift();
    }
    PriorityQueue.prototype.front = function () {
      return this.items[0]
    }
    PriorityQueue.prototype.size = function () {
      return this.items.length;
    }
    PriorityQueue.prototype.isEmpty = function () {
      return this.items.length === 0
    }
    PriorityQueue.prototype.toString = function () {
      let result
      for (let i = 0; i < this.items.length; i++) {
        result += this.items[i].element
      }
      return result;
    }
  }

  let queue = new PriorityQueue();

  queue.enqueue("zhangsan", 10);
  queue.enqueue("lisi", 5);
  queue.enqueue("wangwu", 20)
  queue.enqueue("zhaoliu", 8)

  console.log(queue)

</script>
</body>
</html>